package com.cos.lession72.web.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: COS
 * @time: 2022/9/29 17:12
 * @description:
 */
@Component
public class ErrorController implements HandlerExceptionResolver  {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
                                         Object handler, Exception ex) {
        ModelAndView mv  = new ModelAndView();
        if(ex instanceof NullPointerException){
            mv.addObject("msg", "^_^~ 哎嘿 出错了");
            mv.setViewName("500");
        }else if(ex instanceof IndexOutOfBoundsException){
            mv.addObject("msg", "你越界了了 红牌出场 ");
            mv.setViewName("400");
        }else{
            mv.addObject("msg", "出现异常,请联系管理员,联系方式:不知道");
            mv.setViewName("error");
        }
        return mv;
    }
}
